package com.hhkj.admin.conf.common.validator.commission;

import cn.hutool.core.util.ObjectUtil;
import com.hhkj.admin.common.validator.AbstractValidatorChain;
import com.hhkj.admin.conf.common.cont.CommissionRateConst;
import com.hhkj.admin.conf.model.extend.CommissionRate;
import com.hhkj.admin.conf.model.req.CommissionRateReq;
import org.springframework.stereotype.Component;

import javax.validation.ConstraintValidatorContext;
import java.util.Date;

@Component
public class EffectiveTimeValidator extends AbstractCommissionRateValidatorChain{
    @Override
    protected boolean doValidate(CommissionRateReq bean, ConstraintValidatorContext constraintValidatorContext, AbstractValidatorChain validatorChain) {
        if (CommissionRateConst.ImmediateFlag.IMMEDIATE.getCode().equals(bean.getImmediateFlag()) == false) {
            Date effectiveTime = bean.getEffectiveTime();
            if (ObjectUtil.isEmpty(effectiveTime)) {
                constraintValidatorContext
                        .buildConstraintViolationWithTemplate("生效时间不能为空")
                        .addPropertyNode("effectiveTime")
                        .addConstraintViolation();
                return false;
            }
            if (new Date().compareTo(effectiveTime) >= 0) {
                constraintValidatorContext
                        .buildConstraintViolationWithTemplate("生效时间不能早于今天")
                        .addPropertyNode("effectiveTime")
                        .addConstraintViolation();
                return false;
            }
        }
        return validatorChain.validateNext(bean, constraintValidatorContext);
    }
}
